<?php
include 'util.php';
include 'db_util.php';

$check_failed = false;

header("Content-Type: text/html; charset=utf-8");

if(isset($_REQUEST["email"]) && isset($_REQUEST["email2"]))
{
//    if(fof_authenticate($_REQUEST["user_name"],md5($_REQUEST["user_password"])))
	if($_REQUEST["email"]!=$_REQUEST["email2"])
    {
		$check_failed = true;
		$fpwd_err_msg = "Please double check your email address! ";				
    }
	 elseif (!checkEmail($_REQUEST["email"]))
    {
    	$check_failed = true;
		$fpwd_err_msg = "Please enter a valid email address. example: username@gmail.com";
    }
	else
	{
		// authenticate email and get sid
		$sid = db_authenticate_email($email);
		if($sid){
    		echo "Email found in DB!!<br>";
			$pwd = db_get_pwd($email);
			// send email
//			if(mail($to = $email, $subject = "CircleU: Password Restore", $message = "Your password is: ".$pwd)){
//			if(false) {
			if(send_email($_REQUEST["email"], "Your password is: " . $pwd)) {
				//http_redirect("restore_password.php", array("sid" => $row), true);
				header('Location: EmailSent.php');
			}
			else{
			//	echo "Mail failed. Password is: " . $pwd;
				header('Location: EmailSent.php');
			}
		}
		else{
			$check_failed = true;
			$fpwd_err_msg = "Email is NOT found in Database. Click <a href = 'register.php'>here</a> to register.";
		}
	}
}

// check if an email exists in DB
/*
function db_authenticate_email($email){
	putenv("ORACLE_HOME=/usr/local/libexec/oracle/app/oracle/product/11.2.0/client_1");
	$connection = oci_new_connect($username = 'yqiao',
  	    $password = 'chang526',
        $connection_string = '//oracle.cise.ufl.edu/orcl');
	$query = "SELECT SID FROM STUDENT where Email = '".$email."'";
	$statement = oci_parse($connection, $query);
	oci_execute($statement);
	$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
	oci_free_statement($statement);
	oci_close($connection);
	return $row["SID"];	
}*/
function db_authenticate_email($email) {
	$con = DbConnect();
	$query = "SELECT SID FROM STUDENT where Email = '" . $email . "'";
	$statement = oci_parse($con, $query);
	oci_execute($statement);
	$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
	DbConnectionClose($con, $statement);
	return $row["SID"];	
}

function db_get_pwd($email){
	$query = "select PASSWORD from STUDENT where Email = '" . $email . "'";
//	echo "query: " . $query . "<br>";
	$connection = DbConnect();
	$statement = oci_parse($connection, $query);
	oci_execute($statement);
	$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
	DbConnectionClose($connection, $statement);
	return $row["PASSWORD"];
}

/*
function send_mail2($email){
	define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
	require_once 'XPertMailer/MAIL.php'; // path to 'MAIL.php' file from XPM4 package

	$m = new MAIL; // initialize MAIL class
	$m->From('qiaoyan@ufl.edu'); // set from address
	$m->AddTo($email); // add to address
	$m->Subject('CircleU: Password Restore'); // set subject
	$m->Text('Your password is: '. '123456'); // set text message

	// send mail local and print result
	$status =  $m->Send();
	echo $status? 'Mail sent !' : 'Error when sending email !';

	print_r($m->History); // optional, for debugging
	return $status;
}
*/
function send_email($email, $msg) {
// manage errors
error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors

// path to 'SMTP.php' file from XPM4 package
require_once 'XPertMailer/SMTP.php';

$f = 'yqiao1027@gmail.com'; // from (Gmail mail address)
$t = $email; // to mail address
$p = 'qiaoyan95'; // Gmail password

// standard mail message RFC2822
$m = 'From: '.$f."\r\n".
     'To: '.$t."\r\n".
     'Subject: CircleU - Password Restore'."\r\n".
     'Content-Type: text/plain'."\r\n\r\n".
     $msg;

// connect to MTA server (relay) 'smtp.gmail.com' via SSL (TLS encryption) with authentication using port '465' and timeout '10' secounds
// make sure you have OpenSSL module (extension) enable on your php configuration
$c = SMTP::connect('smtp.gmail.com', 465, $f, $p, 'tls', 10) or die(print_r($_RESULT));

// send mail relay
$s = SMTP::send($c, array($t), $m, $f);

// print result
if ($s) echo 'Sent !';
else print_r($_RESULT);

// disconnect
SMTP::disconnect($c);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>CircleU</title>
<style type="text/css">
<!--
.try {
	background-color: #FFFFFF;
	position: absolute;
	z-index: auto;
	visibility: visible;
	clip: rect(auto,auto,auto,auto);
	top: 5px;
	width: 902px;
	height: 59px;
	left: 83px;
}
.copyright {
	position: absolute;
	left: 150px;
	top: 600px;
	font-size: large;
	height: 22px;
}
.button2 {
	color: #FFFFFF;
	background-color: #0000FF;
	font-family: "Comic Sans MS";
}
.table2 {
	position: absolute;
	left: 433px;
	top: 117px;
}
.CircleU {
	font-family: "Comic Sans MS";
	font-size: xx-large;
	font-weight: bold;
	position: absolute;
	left: 6px;
	top: 13px;
}
.table {
	position: absolute;
	left: 103px;
	top: 103px;
}
#Layer1 {
	position:absolute;
	width:424px;
	height:718px;
	z-index:1;
	left: 481px;
	top: 114px;
}
.STYLE7 {font-size: small}
#RG {
	position: absolute;
	left: 34px;
	top: 116px;
}
#Title1 {
	position: absolute;
	left: 2px;
	top: 4px;
}
#Layer2 {
	position:absolute;
	width:133px;
	height:53px;
	z-index:1;
	left: 6px;
	top: 4px;
}
#Title {
	position:absolute;
	width:1092px;
	height:60px;
	z-index:2;
	left: 9px;
	top: 4px;
	background-color: #FF6600;
}
.STYLE11 {font-family: "Comic Sans MS"; font-size: xx-large; font-weight: bold; position: absolute; left: 22px; top: 6px; color: #0000CC; }
#LayerShow {
	position: absolute;
	left: 265px;
	top: 126px;
	width: 466px;
	height: 116px;
}
-->
</style>
</head>
<body>
<center>
<div id="Title">
  <table width="1091" border="0">
    <tr>
      <td width="312" rowspan="3"><div align="left"><span class="STYLE11">CircleU</span></div></td>
      <td width="51">&nbsp;</td>
      <td width="51">&nbsp;</td>
      <td width="51">&nbsp;</td>
      <td width="51">&nbsp;</td>
      <td width="430">&nbsp;</td>
      <td width="99"><a href="ContactUs.php">Contact Us</a> </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="23">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</div>
<center>
  </div>
  <div class="copyright">
<p>Copyright @ 2011 CircleU built in University of Florida. All right reserved&#13;</p>

</div> <body>
<div id="LayerShow">
	<form action="ForgotPassword.php" method="POST" style="display: inline">
		<center><a href="index.php" style="font-size: 20px; font-family: georgia;">Password Restore</a></center><br>
		<?php
		echo "Your email address:<br><input type=string name=email style='font-size: 16px' value=";
		if(isset($_REQUEST["email"]))
			echo $_REQUEST["email"]."><br><br>"; 
		else
			echo "><br><br>";
		echo "Confirm email address:<br><input type=string name=email2 style='font-size: 16px' value=";
		if(isset($_REQUEST["email2"]))
			echo $_REQUEST["email2"]."><br><br>"; 
		else
			echo "><br><br>";
		?>
		<input type=submit value="Submit" style='font-size: 16px; float: center;'><br>
		<?php if($check_failed) echo "<br><center><font color=red><b>" .$fpwd_err_msg ."</b></font></center>"; ?>
	</form>
</div>
</body>
</center>
</body>
</html>
